package cn.csg.iotplatform.module.system.controller.admin.manufacturer;

import cn.csg.iotplatform.module.common.annotations.CacheResult;
import cn.csg.iotplatform.module.common.annotations.ClearCache;
import cn.csg.iotplatform.module.common.annotations.TranslateMethod;
import cn.csg.iotplatform.module.common.enums.CacheModule;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.csg.iotplatform.framework.common.pojo.PageParam;
import cn.csg.iotplatform.framework.common.pojo.PageResult;
import cn.csg.iotplatform.framework.common.pojo.CommonResult;
import cn.csg.iotplatform.framework.common.util.object.BeanUtils;
import static cn.csg.iotplatform.framework.common.pojo.CommonResult.success;

import cn.csg.iotplatform.framework.excel.core.util.ExcelUtils;

import cn.csg.iotplatform.framework.operatelog.core.annotations.OperateLog;
import static cn.csg.iotplatform.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.csg.iotplatform.module.system.controller.admin.manufacturer.vo.*;
import cn.csg.iotplatform.module.system.dal.dataobject.manufacturer.ManufacturerDO;
import cn.csg.iotplatform.module.system.service.manufacturer.ManufacturerService;

@Tag(name = "管理后台 - 厂商信息")
@RestController
@RequestMapping("/system/manufacturer")
@Validated
public class ManufacturerController {

    @Resource
    private ManufacturerService manufacturerService;

    @PostMapping("/create")
    @Operation(summary = "创建厂商信息")
    @PreAuthorize("@ss.hasPermission('system:manufacturer:create')")
    public CommonResult<String> createManufacturer(@Valid @RequestBody ManufacturerSaveReqVO createReqVO) {
        return success(manufacturerService.createManufacturer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新厂商信息")
    @PreAuthorize("@ss.hasPermission('system:manufacturer:update')")
    public CommonResult<Boolean> updateManufacturer(@Valid @RequestBody ManufacturerSaveReqVO updateReqVO) {
        manufacturerService.updateManufacturer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除厂商信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:manufacturer:delete')")
    public CommonResult<Boolean> deleteManufacturer(@RequestParam("ids") String ids) {
        manufacturerService.deleteManufacturer(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得厂商信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ManufacturerVO> getManufacturer(@RequestParam("id") String id) {
        return success(manufacturerService.getManufacturerDetail(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得厂商信息分页")
    @TranslateMethod
    public CommonResult<PageResult<ManufacturerRespVO>> getManufacturerPage(@ParameterObject @Valid ManufacturerPageReqVO pageReqVO) {
        PageResult<ManufacturerDO> pageResult = manufacturerService.getManufacturerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ManufacturerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出厂商信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:manufacturer:export')")
    @OperateLog(type = EXPORT)
    public void exportManufacturerExcel(@Valid ManufacturerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ManufacturerDO> list = manufacturerService.getManufacturerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "厂商信息.xls", "数据", ManufacturerRespVO.class,
                        BeanUtils.toBean(list, ManufacturerRespVO.class));
    }

    @PostMapping("/checkUnique")
    @Operation(summary = "校验厂商编号和名称唯一性")
    public CommonResult<ManufacturerUniqueRespVO> checkUnique(@Valid @RequestBody ManufacturerUniqueReqVO reqVO) {
        return success(manufacturerService.checkUnique(reqVO));
    }

    @GetMapping("/getConstructionManufacturerList")
    public CommonResult<PageResult<ManufacturerSimpleRespVO>> getConstructionManufacturerList() {
        List<ManufacturerDO> list = manufacturerService.getConstructionManufacturerList();
        return success(BeanUtils.toBean(new PageResult(list,(long)list.size()),ManufacturerSimpleRespVO.class));
    }

    @GetMapping("/syncManufacturerList")
    public CommonResult<Boolean> syncManufacturerList() {
        manufacturerService.syncManufacturerList();
        return success(true);
    }

}